//
//  HYRechargeView.m
//  HaiYue
//
//  Created by skyline on 2018/6/13.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYRechargeView.h"
#import "HYUserFuncButton.h"
NSString *const HYRechargeViewRecharge = @"HYRechargeViewRecharge";

CGFloat const HYRECHARGEBTNHEIGHT = 60;
CGFloat const HYCUSTORMTEXTFIELDHEIGHT = 44;

@interface HYRechargeView()<HYTextFieldDelegate>
@property (nonatomic, strong) HYLabel *balanceLBL;//我的钱包
@property (nonatomic, strong) HYView *line;//
@property (nonatomic, strong) HYLabel *titleLBL;
@property (nonatomic, strong) HYTextField *customTextField;
@property (nonatomic, strong) HYLabel *tipsLBL;
@property (nonatomic, strong) HYUserFuncButton *zfbBtn;
@property (nonatomic, strong) HYUserFuncButton *wxBtn;
@end
@implementation HYRechargeView
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        //        self.tipsLBL.hidden = NO;
        [self setupSubBtn];
        self.zfbBtn.hidden = self.wxBtn.hidden = NO;
    }
    return self;
}

- (HYLabel *)balanceLBL
{
    if (!_balanceLBL)
    {
        _balanceLBL = [[HYLabel alloc] initWithText:@"我的钱包余额:" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight]];
        [self addSubview:_balanceLBL];
        [_balanceLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.top.equalTo(self).with.offset(GAP);
        }];
    }
    return _balanceLBL;
}

- (HYView *)line
{
    if (!_line)
    {
        _line = [[HYView alloc] initWithColor:[UIColor clearColor]];
        [self addSubview:_line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.top.equalTo(self.balanceLBL.mas_bottom).with.offset(GAP);
            make.height.mas_offset(0.5);
        }];
    }
    return _line;
}

- (HYLabel *)titleLBL
{
    if (!_titleLBL)
    {
        _titleLBL = [[HYLabel alloc] initWithText:@"选择充值金额" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight]];
        [self addSubview:_titleLBL];
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.line.mas_bottom).with.offset(GAP);
        }];
    }
    return _titleLBL;
}

- (HYTextField *)customTextField
{
    if (!_customTextField)
    {
        _customTextField = [[HYTextField alloc] initWithPlaceholder:@"自定义金额(1~9999)"];
        _customTextField.layer.borderColor = THEME_COLOR.CGColor;
        _customTextField.layer.borderWidth = CellBottomLineHeight;
        _customTextField.type = HYTextFieldTypePhone;
        _customTextField.textAlignment = NSTextAlignmentCenter;
        _customTextField.placeholderColor = TEXT_GRAY_COLOR;
        _customTextField.placeholderFont = [UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight];
        _customTextField.hydelegate = self;
        [self addSubview:_customTextField];
        [_customTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.titleLBL.mas_bottom).with.offset(HYRECHARGEBTNHEIGHT * 2 + GAP * 3);
            make.width.mas_offset(SCREEN_WIDTH / 2);
            make.height.mas_offset(HYCUSTORMTEXTFIELDHEIGHT);
        }];
    }
    return _customTextField;
}

- (HYLabel *)tipsLBL
{
    if (!_tipsLBL)
    {
        _tipsLBL = [[HYLabel alloc] initWithText:@"1平台币=1元人民币" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight]];
        [self addSubview:_tipsLBL];
        [_tipsLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.customTextField.mas_bottom).with.offset(GAP);
        }];
    }
    return _tipsLBL;
}

- (HYUserFuncButton *)zfbBtn
{
    if (!_zfbBtn)
    {
        _zfbBtn = [[HYUserFuncButton alloc] initWithFrame:CGRectMake(0, 0, HYUserCenterButtonWidth, HYUserCenterButtonWidth)
                                                    title:@"支付宝" image:@"pay_zhifubao_nomal" selectImage:@"pay_zhifubao_selecter"];
        _zfbBtn.titleColor = THEME_COLOR;
        _zfbBtn.tag = 201;
        _zfbBtn.font = [UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight];
        [_zfbBtn addTarget:self action:@selector(actionForZF:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_zfbBtn];
        [_zfbBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(self.zfbBtn.bounds.size);
            make.top.equalTo(self.tipsLBL.mas_bottom).with.offset(GAP);
            make.centerX.equalTo(self).with.offset(-SCREEN_WIDTH / 6.0);
        }];
    }
    return _zfbBtn;
}

- (HYUserFuncButton *)wxBtn
{
    if (!_wxBtn)
    {
        _wxBtn = [[HYUserFuncButton alloc] initWithFrame:CGRectMake(0, 0, HYUserCenterButtonWidth, HYUserCenterButtonWidth)
                                                   title:@"微信" image:@"pay_weixin_nomal" selectImage:@"pay_weixin_selecter"];
        _wxBtn.titleColor = THEME_COLOR;
        _wxBtn.tag = 202;
        _wxBtn.font = [UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight];
        [_wxBtn addTarget:self action:@selector(actionForZF:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_wxBtn];
        [_wxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(self.wxBtn.bounds.size);
            make.top.equalTo(self.tipsLBL.mas_bottom).with.offset(GAP);
            make.centerX.equalTo(self).with.offset(SCREEN_WIDTH / 6.0);
        }];
    }
    return _wxBtn;
}

- (void)setBalance:(NSString *)balance
{
    if (_balance != balance)
    {
        _balance = balance;
        self.balanceLBL.text = [NSString stringWithFormat:@"%@%@",self.balanceLBL.text,_balance];
    }
}
- (void)setupSubBtn
{
    NSArray *array = @[@"5", @"10", @"50", @"100", @"500", @"1000"];
    CGFloat  left_right_gap = (SCREEN_WIDTH - HYRECHARGEBTNHEIGHT * 3.0 - GAP * 2) / 2.0;//到屏幕两边的距离
    for (int i = 0; i < array.count; i++)
    {
        int row = i / 3;//行
        int column = i % 3; //列
        HYButton *button = [[HYButton alloc]initWithFrame:CGRectMake(0, 0, HYRECHARGEBTNHEIGHT, HYRECHARGEBTNHEIGHT) title:array[i] titleColor:ORANGE_COLOR];
        button.touchColor = ORANGE_COLOR;
        button.backgroundColor = [UIColor whiteColor];
        [button addTarget:self action:@selector(actionForZFNumber:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        button.titleLabel.font = [UIFont systemFontOfSize:ContentFont];
        button.type = CCShapeLayerTypeCycle;
        button.layer.borderColor = [UIColor whiteColor].CGColor;
        button.tag = 300 + i;
        [self addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(left_right_gap + (HYRECHARGEBTNHEIGHT + GAP) * column);
            make.top.equalTo(self.titleLBL.mas_bottom).with.offset(GAP + (HYRECHARGEBTNHEIGHT + GAP) * row);
            make.size.mas_equalTo(button.bounds.size);
        }];
    }
}
- (void)actionForZF:(HYUserFuncButton *)sender
{
    [CCNotice post:HYRechargeViewRecharge obj:nil info:@{@"money":self.customTextField.text,@"index":@(sender.tag)}];
}

- (void)actionForZFNumber:(HYButton *)sender
{
    sender.layer.borderColor = ORANGE_COLOR.CGColor;
    sender.selected = YES;
    sender.backgroundColor = ORANGE_COLOR;
    self.customTextField.text = [sender titleForState:UIControlStateNormal];
    for (UIView *obj in [self subviews])
    {
        if (obj.tag >= 300 && obj.tag != sender.tag)
        {
            HYButton *button = (HYButton *)obj;
            button.backgroundColor = [UIColor whiteColor];
            button.layer.borderColor = THEME_COLOR.CGColor;
            button.selected = NO;
        }
    }
}

#pragma mark - HYTextFieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.customTextField.layer.borderColor = THEME_COLOR.CGColor;
    self.customTextField.layer.borderWidth = CellBottomLineHeight;
}

- (void)heightWhenKeyboardChange:(CGFloat)height animationInterval:(float)time
{
    if (height > 0)
    {
        [UIView animateWithDuration:time animations:^{
            self.transform = CGAffineTransformMakeTranslation(0, -height);
        }];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    self.transform = CGAffineTransformIdentity;
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([textField.text isEqualToString:@""] || [string isEqualToString:@""]) return YES;
    else if ([string isNumber] && textField.text.intValue >= 1 && textField.text.intValue <= 999) return YES;
    else return NO;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.customTextField resignFirstResponder];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
